package com.cuij.servicelmpl;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cuij.dao.orderGoodsDao;
import com.cuij.entity.Result;
import com.cuij.entity.orderGoods;
import com.cuij.service.orderGoodsService;
import com.cuij.util.UUIDUtil;

@Service
public class orderGoodsServicelmpl implements orderGoodsService {

    @Resource
    private orderGoodsDao ordergoodsdao;

    // 添加商品操作
    public Result goodsadd(String busi_id, String goods_name, String goods_desc, String goods_pic, double goods_price) {
        Result result = new Result();
        // 生产goods_id
        String goods_id = UUIDUtil.creatUUID();
        // 实例化对象
        orderGoods ordergoods = new orderGoods();
        // 初始化数据
        double goods_level = 0;
        int goods_count = 0;
        ordergoods.setGoods_id(goods_id);
        ordergoods.setBusi_id(busi_id);
        ordergoods.setGoods_name(goods_name);
        ordergoods.setGoods_desc(goods_desc);
        ordergoods.setGoods_pic(goods_pic);
        ordergoods.setGoods_price(goods_price);
        ordergoods.setGoods_level(goods_level);
        ordergoods.setGoods_count(goods_count);
        ordergoodsdao.addgoods(ordergoods);
        // 返回成功状态处理
        result.setData(busi_id);
        result.setStatus(0);
        result.setMsg("添加成功");
        return result;
    }

    // 查询操作
    public Result querygoods(String busi_id) {
        Result result = new Result();
        // 接受查询出的值
        List<Map<String, Object>> goodslist = ordergoodsdao.querygoods(busi_id);
        if (goodslist == null || goodslist.size() == 0) {
            result.setStatus(1);
            result.setMsg("该商户暂无菜品信息");
        }
        else {
            // 获取所需的值
            result.setData(goodslist);
            result.setStatus(0);
            result.setMsg("查询成功");
        }

        return result;
    }

    // 删除产品操作
    public Result deletegoods(String goods_id) {
        Result result = new Result();
        ordergoodsdao.deletegoods(goods_id);
        result.setStatus(0);
        result.setMsg("删除成功");
        return result;
    }

    public Result querysimplegoods(String goods_id) {
        Result result = new Result();
        Map goodsinfo = ordergoodsdao.querysimplegoods(goods_id);
        if (goods_id == null) {
            result.setStatus(1);
            result.setMsg("未查到该产品信息");
        }
        else {
            // 获取所需的值
            result.setData(goodsinfo);
            result.setStatus(0);
            result.setMsg("查询成功");
        }
        return result;
    }

    // 修改商品信息
    public Result modifygoods(String goods_id, String goods_name, String goods_desc, String goods_pic,
            double goods_price, String oldfilename) {
        Result result = new Result();
        orderGoods ordergoods = new orderGoods();
        orderGoods nopicgoods = new orderGoods();

        if (goods_pic != null && goods_pic != "") {
            ordergoods.setGoods_id(goods_id);
            ordergoods.setGoods_name(goods_name);
            ordergoods.setGoods_desc(goods_desc);
            ordergoods.setGoods_price(goods_price);
            ordergoods.setGoods_pic(goods_pic);
            ordergoodsdao.modifygoods(ordergoods);
            // 删除旧文件
            File oldfile = new File("D:/MeCode/SchoolOrder/WebRoot/goodspics/" + oldfilename);
            if (oldfile.isFile() && oldfile.exists()) {
                oldfile.delete();
            }
        }
        else {
            nopicgoods.setGoods_id(goods_id);
            nopicgoods.setGoods_name(goods_name);
            nopicgoods.setGoods_desc(goods_desc);
            nopicgoods.setGoods_price(goods_price);
            ordergoodsdao.modifygoods(nopicgoods);
        }

        result.setStatus(0);
        result.setMsg("修改成功");
        return result;
    }

    // 按不同条件查询商品
    public Result querygoodsorderby(String busi_id, String condition) {
        Result result = new Result();
        List<Map<String, Object>> goodslist = new ArrayList<Map<String, Object>>();
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("busi_id", busi_id);
        if ("商品价格".equals(condition)) {
            param.put("condition", "GOODS_PRICE");
        }
        if ("商品销量".equals(condition)) {
            param.put("condition", "GOODS_COUNT");
        }
        if ("商品评价".equals(condition)) {
            param.put("condition", "GOODS_LEVEL");
        }
        goodslist = ordergoodsdao.querygoodsorderby(param);

        if (null != goodslist && goodslist.size() > 0) {
            result.setData(goodslist);
        }
        return result;
    }
}
